#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ~/Desktop/workspaces/python-tool/shell/tts-generate.py

import subprocess
import time
import sys

def run_adb_command(command):
    """
    执行 ADB 命令
    """
    try:
        result = subprocess.run(command, shell=True, capture_output=True, text=True, check=True)
        return result.stdout
    except subprocess.CalledProcessError as e:
        print(f"ADB command failed: {e}")
        print(f"Error output: {e.stderr}")
        return None

def main():
    array = [
        "车位搜索中",
        "请向前行驶，开始搜索车位",
        "车速过快，请降至30km/h以下",
        "已搜索到车位，请停车",
        "请选择泊车位",
        "请松开制动踏板",
        "请松开方向盘",
        "请系好安全带",
        "请关好车门",
        "请连接手机蓝牙，根据系统提示操作遥控泊车辅助",
        "请切换至P挡，并拉起电子驻车",
        "正在连接手机，请打开ARCFOX极狐手机应用继续 ",
        "请连接手机蓝牙，根据系统提示操作遥控泊车辅助",
        "泊车中，请注意周边环境",
        "泊车已暂停，请关闭车门",
        "泊车已暂停，请系好安全带",
        "泊车已暂停，请移除障碍物",
        "泊车已暂停，请关闭后尾门",
        "泊车已暂停，请关闭前机舱盖",
        "泊车已完成",
        "踩下油门踏板，泊车已退出",
        "干预档位，泊车已退出",
        "干预方向盘，泊车已退出",
        "拉起电子驻车，泊车已退出",
        "换挡次数过多，泊车已退出",
        "泊车超时",
        "系统受限，泊车已退出",
        "坡度过大，泊车已退出",
        "暂停次数过多，泊车已退出",
        "暂停超时，泊车已退出",
        "车轮被阻挡，泊车已退出",
        "路径规划失败，泊车已退出",
        "操作超时，泊车已退出",
        "车速过快，泊车已退出",
        "关联系统受限，泊车已退出",
        "泊车已退出",
    ]

    # 创建目录（如果需要）
    # run_adb_command("adb shell mkdir -p /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/123/")
    
    for i in range(len(array)):
        print(f"Processing item {i+1}/{len(array)}: {array[i]}")
        
        # 发送广播
        broadcast_cmd = f'adb shell am broadcast -a com.aispeech.lyra.action --es key speak --es val "{array[i]}" -f 0x01000000'
        print(f"Sending broadcast: {array[i]}")
        run_adb_command(broadcast_cmd)
        
        # 等待6秒
        print("Waiting 6 seconds...")
        time.sleep(6)
        
        # 创建目录
        # mkdir_cmd = f'adb shell mkdir -p "/sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/{array[i]}"'
        # run_adb_command(mkdir_cmd)
        
        # 移动文件
        mv_cmd = f'adb shell mv -f /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/[0-9]*.mp3 "/sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/{array[i]}.mp3"'
        print(f"Moving file to: {array[i]}/{array[i]}.mp3")
        run_adb_command(mv_cmd)
        
        # 等待2秒
        print("Waiting 2 seconds...")
        time.sleep(2)
        
        print("-" * 50)
    
    print("All TTS generation completed!")

if __name__ == "__main__":
    main()
